﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace ZuesControls.Controls
{
    /// <summary>
    /// 下拉按钮 
    /// </summary>
    [TemplatePart(Name = DropDownMenuItemName, Type = typeof(MenuItem))]
    public class ZSDropDownButton:Button
    {
        const string DropDownMenuItemName = "PART_DropDownMenuItem";

        /// <summary>
        /// 是否打开下拉菜单
        /// </summary>
        public static readonly DependencyProperty IsDropDownOpenProperty =
            DependencyProperty.Register("IsDropDownOpen", typeof(bool), typeof(ZSDropDownButton), new PropertyMetadata(false, OnIsDropDownOpenChanged));

        /// <summary>
        /// 是否显示分割线
        /// </summary>
        public static readonly DependencyProperty IsShowSeparatorProperty =
            DependencyProperty.Register("IsShowSeparator", typeof(bool), typeof(ZSDropDownButton), new PropertyMetadata(false));

        /// <summary>
        /// 下拉菜单
        /// </summary>
        public static readonly DependencyProperty DropDownMenuProperty =
            DependencyProperty.Register("DropDownMenu", typeof(ContextMenu), typeof(ZSDropDownButton), new PropertyMetadata(null, OnDropDownMenuChanged));

        /// <summary>Identifies the <see cref="DropDownMenuStyle"/> dependency property.</summary>
        public static readonly DependencyProperty DropDownMenuStyleProperty = DependencyProperty.Register(
            nameof(DropDownMenuStyle), typeof(Style), typeof(ZSDropDownButton), new PropertyMetadata(null));

        public static readonly DependencyProperty DropDownContentProperty =
          DependencyProperty.Register(nameof(DropDownContent), typeof(ContentControl), typeof(ZSDropDownButton), new PropertyMetadata(null, OnDropDownContentChanged));

        private static void OnDropDownContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
          
        }

        public ContentControl DropDownContent
        {
            get => (ContentControl)GetValue(DropDownContentProperty);
            set => SetValue(DropDownContentProperty, value);
        }

        public static readonly DependencyProperty UsePopupProperty =
            DependencyProperty.Register(nameof(UsePopup), typeof(bool), typeof(ZSDropDownButton), new PropertyMetadata(false));
        private ToggleButton? _toggbutton;
        private Popup? _popUp;

        public bool UsePopup
        {
            get => (bool)GetValue(UsePopupProperty);
            set => SetValue(UsePopupProperty, value);
        }
        /// <summary>
        /// Gets or sets the <see cref="Style"/> used for each item in the options.
        /// </summary>
        public Style DropDownMenuStyle
        {
            get => (Style)this.GetValue(DropDownMenuStyleProperty);
            set => this.SetValue(DropDownMenuStyleProperty, value);
        }

        static ZSDropDownButton()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ZSDropDownButton), new FrameworkPropertyMetadata(typeof(ZSDropDownButton)));
        }

        /// <summary>
        /// 是否打开下拉菜单
        /// </summary>
        public bool IsDropDownOpen
        {
            get { return (bool)GetValue(IsDropDownOpenProperty); }
            set { SetValue(IsDropDownOpenProperty, value); }
        }

        /// <summary>
        /// 是否显示分割线
        /// </summary>
        public bool IsShowSeparator
        {
            get { return (bool)GetValue(IsShowSeparatorProperty); }
            set { SetValue(IsShowSeparatorProperty,value); }
        }

        /// <summary>
        /// 下拉菜单
        /// </summary>
        public ContextMenu DropDownMenu
        {
            get { return (ContextMenu)GetValue(DropDownMenuProperty); }
            set { SetValue(DropDownMenuProperty, value); }
        }

        /// <inheritdoc/>
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
         
            if (DropDownMenu is { })
            {
                DropDownMenu.Style = DropDownMenuStyle;
               
                DropDownMenu.PlacementTarget = this;
            }
         
        }

        private void _toggbutton_Checked(object sender, RoutedEventArgs e)
        {
             
        }

        private static void OnDropDownMenuChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var ZSDropDownButton = (ZSDropDownButton)d;
            if (!ZSDropDownButton.UsePopup && ZSDropDownButton.IsLoaded)
            {
                if (e.NewValue is ContextMenu newMenu)
                {
                    newMenu.Style = ZSDropDownButton.DropDownMenuStyle;
                    newMenu.PlacementTarget = ZSDropDownButton;
                }

                if (e.OldValue is ContextMenu oldMenu)
                {
                    oldMenu.Style = null;
                    oldMenu.PlacementTarget = null;
                }
            }
            
        }

        private static void OnIsDropDownOpenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var ZSDropDownButton = (ZSDropDownButton)d;
           
                ZSDropDownButton.DropDownMenu.IsOpen = ZSDropDownButton.IsDropDownOpen;
            

        }
    }
}
